package com.foton.lotteryinfrastructure.infrastru.repository;

import com.foton.lotterydomain.strategy.model.aggregates.StrategyRich;
import com.foton.lotterydomain.strategy.model.vo.AwardBriefVO;
import com.foton.lotterydomain.strategy.model.vo.StrategyBriefVO;
import com.foton.lotterydomain.strategy.model.vo.StrategyDetailBriefVO;
import com.foton.lotterydomain.strategy.repository.IStrategyRepository;
import com.foton.lotteryinfrastructure.infrastru.dao.IAwardDao;
import com.foton.lotteryinfrastructure.infrastru.dao.IStrategyDao;
import com.foton.lotteryinfrastructure.infrastru.dao.IStrategyDetailDao;
import com.foton.lotteryinfrastructure.infrastru.po.Award;
import com.foton.lotteryinfrastructure.infrastru.po.Strategy;
import com.foton.lotteryinfrastructure.infrastru.po.StrategyDetail;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author RenPu
 * @Date 2023/11/24 10:31
 * @Version 1.0
 * @Description: 抽奖策略 -仓储层
 **/

@Component
public class StrategyRepository implements IStrategyRepository {

    @Resource
    private IStrategyDao iStrategyDao;
    @Resource
    private IStrategyDetailDao iStrategyDetailDao;
    @Resource
    private IAwardDao iAwardDao;


    /**
     * 根据strategyId（策略编码）获取抽奖策略
     *
     * @param strategyId
     * @return
     */
    @Override
    public StrategyRich queryStrategyRich(Long strategyId) {
//        //根据策略ID查询，策略主数据，策略详情数据封装返回
        StrategyBriefVO strategyBriefVO = new StrategyBriefVO();
        Strategy strategy = iStrategyDao.queryStrategyInfo(strategyId);
        //对象转换
        BeanUtils.copyProperties(strategy, strategyBriefVO);

        List<StrategyDetail> strategyDetails = iStrategyDetailDao.queryStrategyDetailList(strategyId);
        List<StrategyDetailBriefVO> strategyDetailBriefVOList = new ArrayList<>();
        for (StrategyDetail strategyDetail : strategyDetails) {
            StrategyDetailBriefVO strategyDetailBriefVO = new StrategyDetailBriefVO();
            //对象转换
            BeanUtils.copyProperties(strategyDetail, strategyDetailBriefVO);
            strategyDetailBriefVOList.add(strategyDetailBriefVO);
        }

        return new StrategyRich(strategyId, strategyBriefVO, strategyDetailBriefVOList);
    }


    /**
     * 根据奖品id获取奖品信息
     *
     * @param awardId
     * @return
     */
    @Override
    public AwardBriefVO queryAwardInfo(String awardId) {
        AwardBriefVO awardBriefVO = new AwardBriefVO();
        //对象转换
        BeanUtils.copyProperties(iAwardDao.queryAwardInfo(awardId), awardBriefVO);
        return awardBriefVO;
    }


    /**
     * 查询库存为0的商品ID值，便于在业务逻辑层对抽到此类商品做处理
     *
     * @param strategyId
     * @return
     */
    @Override
    public List<String> queryNoStockStrategyAwardList(Long strategyId) {

        return iStrategyDetailDao.queryNoStockStrategyAwardList(strategyId);
    }


    /**
     * 针对策略详情的，商品数量进行扣减，根据策略ID值，奖品ID作为条件扣减
     *
     * @param strategyId
     * @param awardId
     * @return
     */
    @Override
    public boolean deductStock(Long strategyId, String awardId) {
        StrategyDetail strategyDetail = new StrategyDetail();
        strategyDetail.setStrategyId(strategyId);
        strategyDetail.setAwardId(awardId);
        return iStrategyDetailDao.deductStock(strategyDetail) == 1;
    }
}
